package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleAddParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQueryParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQueryParam vehicleQueryParam) {
        log.debug("车辆查询业务：vehicleQueryParam:{}", vehicleQueryParam);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQueryParam);
//        log.debug("查询结果：{}", list);
        return list;
    }

    @Override
    public void saveVehicle(VehicleAddParam vehicleAddParam) {
        log.debug("车辆新增业务：vehicleAddParam:{}", vehicleAddParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        VehicleVO vehicleVO = vehicleMapper.selectVehicleByLicense(vehicle.getLicense());
        if (vehicle.getId() == null) {
            if (vehicleVO != null) {
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicle.setGeofenceId(0L);
            vehicleMapper.insertVehicle(vehicle);
        } else {
            if (vehicleVO != null && !vehicleVO.getId().equals(vehicle.getId())) {
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("车辆删除业务：id:{}", id);
        vehicleMapper.deleteVehicle(id);
    }

    @Override
    public void unbindGeofence(Long vehicleId) {
        log.debug("解绑车辆业务 vehicleId{}", vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicleMapper.unbindGeofence(vehicle);
    }

    @Override
    public void bindGeofence(Long geofenceId, Long vehicleId) {
        log.debug("绑定车辆业务");
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public void unbindAllByGeofenceId(Long geofenceId) {
        log.debug("删除围栏业务 geofenceId{}",  geofenceId);
        if (geofenceId == null) throw new ServiceException(StatusCode.DATA_UNEXISTS);
        vehicleMapper.unbindAllByGeofenceId(geofenceId);
    }
}
